/**
  ******************************************************************************
  * @file    usart.c
  * @brief   This file provides code for the configuration
  *          of the USART instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */


#include "usart.h"



#include "stdlib.h"
#include "string.h"
#include "main.h"
#include <stdint.h>
#include <stdarg.h>



UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;


extern unsigned char UART1_Rx_Buf[MAX_REC_LENGTH];
extern unsigned int  UART1_Rx_cnt ;
extern unsigned char UART1_temp[REC_LENGTH];



void MX_USART1_UART_Init(void)
{

  

  

  

  
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 9600;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  

  

}


void MX_USART2_UART_Init(void)
{

  

  

  

  
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  

  

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==USART1)
  {
  

  
    
    __HAL_RCC_USART1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  

  
  }
  else if(uartHandle->Instance==USART2)
  {
  

  
    
    __HAL_RCC_USART2_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART2 GPIO Configuration
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  

  
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==USART1)
  {
  

  
    
    __HAL_RCC_USART1_CLK_DISABLE();

    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

    
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  

  
  }
  else if(uartHandle->Instance==USART2)
  {
  

  
    
    __HAL_RCC_USART2_CLK_DISABLE();

    /**USART2 GPIO Configuration
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);

  

  
  }
}



char string[512] = {0};
void SysPrintf(const char *fmt, ...)
{
	va_list ap;

	uint8_t L = 0, i = 0;
	va_start(ap, fmt);
	vsprintf(string, fmt, ap);
	va_end(ap);
	L = strlen(string);
	while (L--)
	{
		User_UART_Sent(string[i++]);
	}
}

void User_UART_Sent(uint8_t val)
{
	HAL_UART_Transmit(&huart2,&val,1,0xFFFF);
}



void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if(huart->Instance==USART1)
  {
    UART1_Rx_Buf[UART1_Rx_cnt] = UART1_temp[0];
    UART1_Rx_cnt++;
		if(UART1_Rx_cnt>=MAX_REC_LENGTH)
			UART1_Rx_cnt = 0;
    HAL_UART_Receive_IT(&huart1,(uint8_t *)UART1_temp,REC_LENGTH);
  }
	else
	{
		
	}
	
}


void SendDataToNBModule(uint8_t *buf,uint8_t len)
{
		buf[len]=0;
		HAL_UART_Transmit(&huart1,buf,len,1000);

}



u32 atou(s8 *str)
{
	const s8 * s = str;
	u64 result = 0;
	
	if (str == NULL) {
		return result;
	}

	while (*s == ' ') {
		s++;
	}

	while (*s >= '0' && *s <= '9' ) {
		result = result * 10 + (*s - '0');
		if ((result & 0xffffffff00000000)) {
			result = result / 10;
			break;
		}
		s++;
	}

	return (u32)result;
}




s32 RtspGetStringContent(s8 *buf, s8 *starttitle,s8 *endtitle, s8 *content)
{
    s8 *start = NULL;
    s8 *end = NULL;
    s32 len = 0;
    if (buf == NULL || starttitle == NULL || content == NULL) {
        SysPrintf("check args null");
        return -1;
    }

    start = strstr((c8*)buf, (c8*)starttitle);
    if (start == NULL) {
        SysPrintf("can't find title:%s", starttitle);
        return -1;
    }

    start += strlen(starttitle);
    while (*start == ' ') {
        start++;
    }

    
    end = strstr(start,endtitle);
    if (end == NULL) {
        
        return -1;
    }

    len = end - start;
    strncpy(content, start, len);
    content[len] ='\0';
    return len;
}







uint32_t UtilGetIntFlag(uint8_t *buf,uint8_t*startflag,uint8_t*endflag)
{
    s8  tmpBuf[128];
    s32 ret;
    memset(tmpBuf, 0, sizeof(tmpBuf));
	
    ret = RtspGetStringContent(buf, startflag,endflag, tmpBuf);
    if (ret > 0) {
        return (u32)atol(tmpBuf);
    }
	
	
    return 0;
}

uint8_t	UtilGetStringFlag(uint8_t *buf,uint8_t*startflag,uint8_t*endflag,uint8_t*content)
{
	memset(content, 0, sizeof(content));
    if (RtspGetStringContent(buf,startflag,endflag,content) > 0)
	{
        return TRUE;
    }

    return FALSE;
} 




